package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.List;

import longcaisuyun.longcai.com.net.PostDaTi;
import wh.utils.StringUtils;



/**
 * Created by Administrator on 2016/6/22.
 * 答题
 */
public class DaTiAdapter extends BaseAdapter {
    private Context mContext;
    private List<PostDaTi.DaTi> list;
    private android.os.Handler mHandler;
    public DaTiAdapter(Context mContext,List<PostDaTi.DaTi> list,android.os.Handler mHandler) {
        this.mContext=mContext;
        this.list=list;
        this.mHandler=mHandler;
    }

    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder holder=null;
        if (convertView==null){
            holder=new viewHolder();
            convertView=View.inflate(mContext, R.layout.listitem_dati,null);
            MyApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);
            holder.tv_timu= (TextView) convertView.findViewById(R.id.tv_timu);
            holder.tv_a= (TextView) convertView.findViewById(R.id.tv_a);
            holder.tv_b= (TextView) convertView.findViewById(R.id.tv_b);
            holder.tv_c= (TextView) convertView.findViewById(R.id.tv_c);
            holder.tv_d= (TextView) convertView.findViewById(R.id.tv_d);
            holder.tv_num= (TextView) convertView.findViewById(R.id.tv_num);
            holder.rb_a= (RadioButton) convertView.findViewById(R.id.rb_a);
            holder.rb_b= (RadioButton) convertView.findViewById(R.id.rb_b);
            holder.rb_c= (RadioButton) convertView.findViewById(R.id.rb_c);
            holder.rb_d= (RadioButton) convertView.findViewById(R.id.rb_d);
            holder.rg_xuan= (RadioGroup) convertView.findViewById(R.id.rg_xuan);
            holder.rg_xuan.setTag(position);
            holder.rb_a.setTag(position);
            holder.rb_b.setTag(position);
            holder.rb_c.setTag(position);
            holder.rb_d.setTag(position);
            convertView.setTag(holder);
        }else{
            holder= (viewHolder) convertView.getTag();
        }
        holder.rg_xuan.setOnCheckedChangeListener(null);
        final PostDaTi.DaTi daTi = list.get(position);
        holder.tv_timu.setText(daTi.getTitle());
        holder.tv_a.setText(daTi.getOptiona());
        holder.tv_b.setText(daTi.getOptionb());
        holder.tv_c.setText(daTi.getOptionc());
        holder.tv_d.setText(daTi.getOptiond());
        holder.tv_num.setText(daTi.getId());
        if (StringUtils.isEmpty(list.get(position).getChecked())){
            Log.e("getChecked", list.get(position).getChecked() + "1");
            holder.rg_xuan.clearCheck();
        }else{
            switch (daTi.getChecked()){
                case "1":
                    holder.rg_xuan.check(R.id.rb_a);
                    break;
                case "2":
                    holder.rg_xuan.check(R.id.rb_b);
                    break;
                case "3":
                    holder.rg_xuan.check(R.id.rb_c);
                    break;
                case "4":
                    holder.rg_xuan.check(R.id.rb_d);
                    break;
            }
        }
        holder.rg_xuan.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_a:
                        daTi.setChecked("1");
                        break;
                    case R.id.rb_b:
                        daTi.setChecked("2");
                        break;
                    case R.id.rb_c:
                        daTi.setChecked("3");
                        break;
                    case R.id.rb_d:
                        daTi.setChecked("4");
                        break;
                }
            }
        });
        return convertView;
    }

//    private void XuanZhong(final int position, int i) {
//        Message msg=new Message();
//        msg.what=i;
//        msg.obj=position;
//        mHandler.sendMessage(msg);
//    }

    class viewHolder{
        private TextView tv_timu,tv_a,tv_b,tv_c,tv_d,tv_num;
        private RadioButton rb_a,rb_b,rb_c,rb_d;
        private RadioGroup rg_xuan;
    }
    //刷新页面
    public void notifyDataSetChanged(List<PostDaTi.DaTi> list) {
        this.list=list;
        super.notifyDataSetChanged();
    }
}
